<?php
class ModuleBase
{
	public static $type = 'master';
	
	/**
	 * 
	 * 插入数据
	 * @param $row
	 * @param $table
	 * @param $reType id|default
	 */
	public function queryInsert( $row, $table, $reType='id' )
	{
		if(!$row || !is_array($row)){
			return false;
		}
		foreach ($row as $key=>$val){
			$fields[] = "`{$key}`";
			$values[] = "'{$val}'";
		}
		$sql = "INSERT INTO `{$table}`(".implode(',', $fields).")VALUES(".implode(',', $values).")";
		$result = Db::db_write()->query($sql);
		if(!$result){
			return FALSE;
		}else{
			return $reType=='id' ? Db::db_write()->getLastInsertId() : $row;
		}
	}
	
	public function queryUpdate($row, $where, $table, $reType='affectRows')
	{
		$sql = self::buildUpdateSql($row, $where, $table);
		$result = Db::db_write()->query($sql);
		if(!$result){
			return false;
		}else{
			return $reType=='affectRows' ? $result : $row;
		}		
	}
	
	public function buildUpdateSql($row, $where, $table)
	{
		if(!$row || !is_array($row)){
			return false;
		}
		foreach ($row as $key=>$val){
			$updates[] = "`{$key}`='" . ($val) . "'";
		}
		if(!$where){
			$wheres[] = "1";
		}elseif (!is_array($where)){
			$wheres[] = $where;
		}else{
			foreach ($where as $key=>$val){
				$wheres[] = "`{$key}`='".($val)."'";
			}
		}
		$sql = "UPDATE `{$table}` SET ".implode(',', $updates)." WHERE " . implode(' and ', $wheres);
		return $sql;
	}
	
	public function queryDelete($where, $table)
	{
		if(!$where || !is_array($where)){
			$wheres[] = $where;
		}else{
			foreach ($where as $key=>$val){
				$wheres[] = "`{$key}`='".($val)."'";
			}
		}
		$sql = "DELETE FROM `{$table}` WHERE " . implode(' and ', $wheres);
		$result = Db::db_write()->query($sql);
		return $result;
	}
	
	public function querySelect($fields, $where, $table, $order=array(), $start=0, $limit=0)
	{
		if(is_array($where)){
			$wheres = array();
			foreach ($where as $key=>$val){
				$wheres[] = "`{$key}`='".($val)."'";
			}
			$where = implode(' and ', $wheres);
		}
		if(is_array($order)){
			$orders = array();
			foreach ($order as $key=>$val){
				$orders[] = "`{$key}` {$val}";
			}
			$order = implode(',', $orders);
		}
		$sql = "SELECT ".implode(',', $fields)." FROM `{$table}` WHERE " . $where;
		if($order){
			$sql .= " ORDER BY {$order}";
		}
		if($limit){
			$sql .= " LIMIT {$start},{$limit}";
		}
		$result = Db::db_read()->query($sql);
		$ret = array();
		if($result){
			while ($row = mysql_fetch_assoc($result)){
				$ret[] = $row;
			}
		}
		return $ret;
	}
	
	public function querySelectSingle($fields, $where, $table)
	{
		if(is_array($where)){
			foreach ($where as $key=>$val){
				$wheres[] = "`{$key}`='".($val)."'";
			}
			$where = implode(' and ', $wheres);
		}
		$sql = "SELECT ".implode(',', $fields)." FROM `{$table}` WHERE " . $where;
		$result = Db::db_read()->query($sql);
		$ret = array();
		if($result){
			$ret = mysql_fetch_assoc($result);
		}
		return $ret;
	}
	
	public function getRows($sql)
	{
		$result = Db::db_read()->query($sql);
		$ret = array();
		if(!$result || empty($result)){
			return $ret;
		}
		while ($row = mysql_fetch_assoc($result)){
			$ret[] = $row;
		}
		return $ret;
	}
	
	public function initDb($type='master')
	{
		self::$type = $type;
	}
	
	public function getRow($sql)
	{
		$result = Db::db_read()->query($sql);
		$row = array();
		if(!$result || empty($result)){
			return $row;
		}
		return mysql_fetch_assoc($result);
	}
	
	public function query($sql)
	{
		return Db::getInstance(self::$type)->query($sql);
	}
	
	public function getLastSql()
	{
		return Db::getInstance(self::$type)->getLastSql();
	}
	
	public function getError()
	{
		return Db::getInstance(self::$type)->getError();
	}
}